(function(fw){
	
	
	function security() {

		var resources;
	
		function getResources() {
			if(!resources || resources.length == 0) {
				resources = JSON.parse(localStorage.getItem("resources") || "[]");
			}
			return resources;
		}
	
		function findResource(code,category) {
			return ko.utils.arrayFirst(getResources(), function(item) {
				return item.code == code && item.category == category;
			});
		}
	
		function findChildrens(menuId,category) {
			return ko.utils.arrayFilter(getResources(), function(item) {
				return item.parentId == menuId && item.category == category;
			});
		}
	
		function processPage(pageName, container, isReturn) {
			if(pageName && container) {
				var parent = findResource(pageName,0);
				if(parent) {
					if(!parent.isAuthenticated) {
						container.find("[data-security-code]").hide();
					} else {
						var childrens = findChildrens(parent.menuId,1);
						$.each(childrens || [], function(index,item) {
							if(!item.isAuthenticated) {
								container.find("[data-security-code=" + item.code + "]").hide();
							}else{
								container.find("[data-security-code=" + item.code + "]").show();
							}
						});
					}
				}
			}
			if(isReturn) {
				return container.html();
			}
		}
	
		function process(page) {
			try {
				if(page === undefined) {
					var pages = $(".page");
					if(pages && pages.length > 0) {
						$.each(pages, function(index,item) {
							processPage($(item).data("page"), $(item));
						});
					}
				} else if(typeof(page) == "object") {
					processPage(page.name, $(page.container));
				} else {
					return processPage($(page).data("page"), $(page), true);
				}
			} catch(e) {
				console.log(e);
			}
		}
	
		return {
			process: function(){}
		}
	};
	fw.security = new security();
}	
)(window.framework);